package toolkits;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA512Test {
    
    static{
        System.out.println("Load SHA512Test!!!!!!!!!!!!!!!!!!!!"+Thread.currentThread().getContextClassLoader());
    }

    public static void main(String[] args) throws Exception {
        go();
    }
    
    public static void go() throws NoSuchAlgorithmException{
        System.out.println("====== After");
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        byte[] dataBytes = new byte[1024];
        
        //before transaction
        //key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5||||||<SALT>
        //dataBytes="xx|2013122001|179.46|Total Shopping|TestFn|a@b.com|1213||||||||||xx".getBytes();
        
        //after transaction
        //sha512(<SALT>|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key)
        //dataBytes="xx|success|||||||||||a@b.comb|TestFn|Total Shopping|12.90|2013121305|xx".getBytes();
        
        //for web service
        //sha512(key|command|var1|salt) 
        //dataBytes="C0Dr8m|cancel_refund_transaction|403993715508783896|xx".getBytes();
        dataBytes="C0Dr8m|verify_payment|2121002700565|3sf0jURk".getBytes();
        
        md.update(dataBytes, 0, dataBytes.length);
        /*
        int nread = 0;
        while ((nread = fis.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        }
        */
        
        byte[] mdbytes = md.digest();
        // convert the byte to hex format method
        StringBuffer sb = new StringBuffer();
        System.out.println( (mdbytes[0] & 0xff) + " "+ Integer.toString(mdbytes[0] & 0xff, 16));
        System.out.println( (0x100) + " "+ Integer.toString(0x100,16));
        for (int i = 0; i < mdbytes.length; i++) {
            sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        System.out.println("Hex format A : " + sb.toString());
        // convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
            hexString.append(Integer.toHexString(0xFF & mdbytes[i]));
        }
        System.out.println("Hex format B : " + hexString.toString());
    }

}
